#!/usr/bin/env python -OO
# -*- coding: utf-8 -*-

# Copyright © 2008,2009 Trust Digital Technology Laboratory, Shanghai Jiao Tong University.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author:
#   XING Dong sheng <homer.xing@gmail.com>

from __future__ import with_statement

import os, sys, stat
import webbrowser
import softwaredependent as D
from softwareindependent import getArch, notify, TempOwn

cwd = os.getcwd()

def installintelcompiler():
	#readlicense
	webbrowser.open('http://tdt.sjtu.edu.cn/S/IntelCompilerSuiteLicense.html', 1, 1) #new=1, autoraise=1
	#installlicense
	f = D.download("intelcompilersuite")
	os.chdir(D.LOCAL)
	D.execute("tar xf %s"%(f))
	D.execute("sudo mkdir -p /opt/intel/licenses/; sudo chmod a+rw NCOM*.lic; sudo mv NCOM*.lic /opt/intel/licenses/")
	#installcompiler
	if getArch()==32:
		D.execute("sudo dpkg -i intel-cproc110069_11.0-2_i386.deb; sudo dpkg -i intel-cproc110069iidb_11.0-2_i386.deb")
		D.execute(r"sudo sed -e 's/!\s\/bin\/sh/!\/bin\/bash/g;' -e 's/<INSTALLDIR>/\/opt\/intel\/Compiler\/11.0\/069/g;' /opt/intel/Compiler/11.0/069/bin/ia32/[cimpstux]* -i")
	else:
		D.execute("sudo dpkg -i intel-cproc110069e_11.0-2_amd64.deb; sudo dpkg -i intel-cproc110069iidbe_11.0-2_amd64.deb")
		D.execute(r"sudo sed -e 's/!\s\/bin\/sh/!\/bin\/bash/g;' -e 's/<INSTALLDIR>/\/opt\/intel\/Compiler\/11.0\/069/g;' /opt/intel/Compiler/11.0/069/bin/intel64/[cimpstux]* -i")
	D.execute("sudo cp %s /usr/local/bin/; sudo chmod a+x /usr/local/bin/useicc"%(D.FILES+"useicc") )
	#installtbb
	D.execute("tar zxf tbb21_20080605oss_src.tgz")
	os.chdir("tbb21_20080605oss")
	D.execute("make >/dev/null 2>/dev/null; sudo cp build/linux_*_release/libtbb*.so* /usr/lib/; sudo cp include/tbb /usr/local/include/tbb -r")
	os.chdir("..")
	#install mkl
	D.execute("sudo cp /bin/sh /bin/sh.back; cd /bin/; sudo ln bash sh -sf")
	D.execute("sudo dpkg -i intel-mkl101015_10.1p-16_all.deb")
	D.execute("sudo mv /bin/sh.back /bin/sh")
	if getArch()==32:
		D.execute("sudo cp /opt/intel/mkl/10.1.0.015/tools/environment/mklvars32.sh /usr/local/bin/usemkl")
	else:
		D.execute("sudo cp /opt/intel/mkl/10.1.0.015/tools/environment/mklvarsem64t.sh /usr/local/bin/usemkl")
	#register
	notify("注意！涉及法律条款！",
"本程序为您安装了一个免费许可证。但请您不要使用它。请您到Intel网站上获取免费使用许可证，并在24小时内 删除 /opt/intel/licenses/NCOM_L_CMP__NF83-JJBCL6XB.lic 文件。获取许可证的网页正在打开")
	webbrowser.open('https://registrationcenter.intel.com/RegCenter/NComForm.aspx?ProductID=1096', 1, 1) #new=1, autoraise=1
	#Back to original directory
	os.chdir(cwd)

if __name__ == "__main__":
	installintelcompiler()
